package com.lc.mail.utils;

import com.study.common.entity.bo.MailBo;
import com.study.common.enums.ReturnCodeEnum;
import com.study.common.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;

/**
 * @description: 邮件发送工具类
 * @author: LChuang
 * @date: 2023/10/7 9:43
 */
@Slf4j
@Component
public class MailUtil {

    /**
     * 邮件发送方
     */
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    private static String sender;

    private static JavaMailSender mailSender;

    @PostConstruct
    public void init(){
        mailSender = javaMailSender;
        sender = from;
    }

    /**
     * 发送邮件 - 不带附件
     */
    // @SneakyThrows(Exception.class)
    public static String sendMail(@Validated MailBo bo){
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(bo.getReceivers());
            helper.setSubject(bo.getSubject());
            // 邮件内容，true表示带有<附件/html>
            helper.setText(bo.getContent(), true);
            mailSender.send(message);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (MessagingException e) {
            log.error("普通邮件发送失败：{}", e.getMessage());
            return ReturnCodeEnum.MAIL_FAIL.getMsg();
        }
    }


    /**
     * 发送邮件 - 带附件
     */
    // @SneakyThrows(Exception.class)
    public static String sendAnnexMail(@Validated MailBo bo, MultipartFile file){
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sender);
            helper.setTo(bo.getReceivers());
            helper.setSubject(bo.getSubject());
            // 邮件内容，true表示带有<附件/html>
            helper.setText(bo.getContent(), true);
            // 文件转化
            File conventFile = FileUtil.multipartFileToFile(file);
            assert conventFile != null;
            FileSystemResource fileResource = new FileSystemResource(conventFile);
            // 添加附件
            helper.addAttachment(Objects.requireNonNull(fileResource.getFilename()), fileResource);
            mailSender.send(message);
            return ReturnCodeEnum.SUCCESS.getMsg();
        } catch (MessagingException e) {
            log.error("附件短信发送失败：{}", e.getMessage());
            return ReturnCodeEnum.MAIL_FAIL.getMsg();
        }
    }

}
